package org.example.short_link.project.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.example.short_link.project.dao.entity.ShortLinkDO;
import org.example.short_link.project.dao.mapper.ShortLinkMapper;
import org.example.short_link.project.dto.req.RecycleBinRecoverDTO;
import org.example.short_link.project.dto.req.RecycleBinRemoveDTO;
import org.example.short_link.project.dto.req.RecycleBinSaveDTO;
import org.example.short_link.project.dto.req.RecyclePageReqDTO;
import org.example.short_link.project.dto.resp.ShortLinkPageRespDTO;
import org.example.short_link.project.service.RecycleBinService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.concurrent.TimeUnit;

import static org.example.short_link.project.common.constant.RedisKeyConstant.GOTO_SHORT_LINK_KEY;
import static org.example.short_link.project.common.constant.RedisKeyConstant.LOCK_GOTO_SHORT_LINK_KEY;

/**
 * 回收站管理接口实现层
 *
 */
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<ShortLinkMapper , ShortLinkDO> implements RecycleBinService {

    private final StringRedisTemplate stringredisTemplate;


    @Override
    public void RecycleBinSave(RecycleBinSaveDTO requesParam) {

        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ShortLinkDO::getFullShortUrl,requesParam.getFullShortUrl())
                .eq(ShortLinkDO::getGid,requesParam.getGid())
                .eq(ShortLinkDO::getDelFlag, 0)
                .eq(ShortLinkDO::getEnableStatus , 0);
        ShortLinkDO shortLinkDO = ShortLinkDO.builder()
                .enableStatus(1)
                .build();
        baseMapper.update(shortLinkDO,updateWrapper);
        stringredisTemplate.delete(
                String.format(GOTO_SHORT_LINK_KEY,requesParam.getFullShortUrl())
        );




    }

    @Override
    public IPage<ShortLinkPageRespDTO> pageRecycle(RecyclePageReqDTO requestParam) {
        LambdaQueryWrapper<ShortLinkDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShortLinkDO::getEnableStatus,1)
                .in(ShortLinkDO::getGid,requestParam.getGidList())
                .eq(ShortLinkDO::getDelFlag , 0)
                .orderByDesc(ShortLinkDO::getUpdateTime);
        // 将 `resultPage` 中的每个 `ShortLinkDO` 对象转换为 `ShortLinkPageRespDTO` 对象，
        // 并返回一个新的 `IPage<ShortLinkPageRespDTO>`
        IPage<ShortLinkDO> resultPage = baseMapper.selectPage(requestParam, queryWrapper);

        return resultPage.convert(each ->{

            ShortLinkPageRespDTO result = BeanUtil.toBean(each, ShortLinkPageRespDTO.class);
            result.setDomain("http://" + result.getDomain());
            return result;

        });
    }

    @Override
    public void RecycleBinRecover(RecycleBinRecoverDTO requestParam) {
        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ShortLinkDO::getFullShortUrl,requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getGid,requestParam.getGid())
                .eq(ShortLinkDO::getDelFlag, 0)
                .eq(ShortLinkDO::getEnableStatus , 1);
        ShortLinkDO shortLinkDO = ShortLinkDO.builder()
                .enableStatus(0)
                .build();
        baseMapper.update(shortLinkDO,updateWrapper);
        //将短链接移至回收站时，会创建key空value以防止缓存击穿。 恢复短链接时需要将空key删除，防止查不到该链接
        stringredisTemplate.delete(String.format(LOCK_GOTO_SHORT_LINK_KEY, requestParam.getFullShortUrl()));



    }

    @Override
    public void RecycleBinRemove(RecycleBinRemoveDTO requestParam) {
        LambdaUpdateWrapper<ShortLinkDO> deleteWrapper = new LambdaUpdateWrapper<>();
        deleteWrapper.eq(ShortLinkDO::getFullShortUrl,requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getGid,requestParam.getGid())
                .eq(ShortLinkDO::getDelFlag, 0)
                .eq(ShortLinkDO::getEnableStatus , 1);
        baseMapper.delete(deleteWrapper);


    }

}
